from flask import Blueprint
from flask import request
from flask import render_template
from flask import session
from flask import redirect
from Education.util.checkLogin import check_login

blue_login = Blueprint('login', __name__)


@blue_login.route('/login', methods=['GET', 'POST'])
def goto_login():
    if request.method == "GET":
        user = session.get('user')
        if not user:
            return render_template('login.html', error='请登录')
        return redirect('index')
    else:
        user = request.form.get('user')
        pwd = request.form.get('pwd')
        name = check_login(user, pwd)
        if not name:
            return render_template('login.html', error='用户名或密码错误')
        else:
            session['user'] = user
            session['pwd'] = pwd
            session['name'] = name
            return redirect('index')
